It shows how to set transmission mode to multicast mode, that is, querying the user to launch the multicast controlling application or the multicast monitoring application, and opening camera and setting multicast mode in the specified access permission by MV_GIGE_SetTransmissionType().
10 currentsystem = platform.system()
11 if currentsystem ==
'Windows':
12 sys.path.append(os.path.join(os.getenv(
'MVCAM_COMMON_RUNENV'),
"Samples",
"Python",
"MvImport"))
14 sys.path.append(os.path.join(
"..",
"..",
"MvImport"))
15 from MvCameraControl_class
import *
18 if sys.version_info[0] < 3:
20 input_func = raw_input
28 Safely decode a string from a ctypes character array. 29 Compatible with Python 2.x and 3.x, as well as 32-bit and 64-bit environments. 31 byte_str = memoryview(ctypes_char_array).tobytes()
34 null_index = byte_str.find(b
'\x00')
36 byte_str = byte_str[:null_index]
39 for encoding
in [
'gbk',
'utf-8',
'latin-1']:
41 return byte_str.decode(encoding)
42 except UnicodeDecodeError:
46 return byte_str.decode(
'latin-1', errors=
'replace')
54 stOutFrame = MV_FRAME_OUT()
55 memset(byref(stOutFrame), 0, sizeof(stOutFrame))
57 ret = cam.MV_CC_GetImageBuffer(stOutFrame, 1000)
58 if None != stOutFrame.pBufAddr
and 0 == ret:
59 print (
"get one frame: Width[%d], Height[%d], nFrameNum[%d]" % (stOutFrame.stFrameInfo.nWidth, stOutFrame.stFrameInfo.nHeight, stOutFrame.stFrameInfo.nFrameNum))
60 cam.MV_CC_FreeImageBuffer(stOutFrame)
62 print (
"no data[0x%x]" % ret)
66 if __name__ ==
"__main__":
70 MvCamera.MV_CC_Initialize()
72 SDKVersion = MvCamera.MV_CC_GetSDKVersion()
73 print (
"SDKVersion[0x%x]" % SDKVersion)
75 deviceList = MV_CC_DEVICE_INFO_LIST()
76 tlayerType = MV_GIGE_DEVICE
79 ret = MvCamera.MV_CC_EnumDevices(tlayerType, deviceList)
81 print (
"enum devices fail! ret[0x%x]" % ret)
84 if deviceList.nDeviceNum == 0:
85 print (
"find no device!")
88 print (
"find %d devices!" % deviceList.nDeviceNum)
90 for i
in range(0, deviceList.nDeviceNum):
91 mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents
92 if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE:
93 print (
"\ngige device: [%d]" % i)
94 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName)
95 print (
"device model name: %s" % strModeName)
96 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chSerialNumber)
97 print(
"device serial number: %s" % strSerialNumber)
98 nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24)
99 nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16)
100 nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8)
101 nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff)
102 print (
"current ip: %d.%d.%d.%d\n" % (nip1, nip2, nip3, nip4))
105 nConnectionNum =
input_func(
"please input the number of the device to connect:")
107 if int(nConnectionNum) >= deviceList.nDeviceNum:
108 print (
"intput error!")
115 stDeviceList = cast(deviceList.pDeviceInfo[int(nConnectionNum)], POINTER(MV_CC_DEVICE_INFO)).contents
117 ret = cam.MV_CC_CreateHandle(stDeviceList)
119 raise Exception (
"create handle fail! ret[0x%x]" % ret)
123 print (
"start multicast sample in (c)ontrol or in (m)onitor mode? (c/m)")
129 if key ==
'm' or key ==
'M':
131 elif key ==
'c' or key ==
'C':
134 raise Exception (
"intput error!")
137 ret = cam.MV_CC_OpenDevice(MV_ACCESS_Monitor, 0)
139 raise Exception (
"open device fail! ret[0x%x]" % ret)
141 ret = cam.MV_CC_OpenDevice(MV_ACCESS_Control, 0)
143 raise Exception (
"open device fail! ret[0x%x]" % ret)
146 if stDeviceList.nTLayerType == MV_GIGE_DEVICE:
147 nPacketSize = cam.MV_CC_GetOptimalPacketSize()
148 if int(nPacketSize) > 0:
149 ret = cam.MV_CC_SetIntValue(
"GevSCPSPacketSize",nPacketSize)
151 print (
"Warning: Set Packet Size fail! ret[0x%x]" % ret)
153 print (
"Warning: Get Packet Size fail! ret[0x%x]" % nPacketSize)
157 device_ip_list = strIp.split(
'.')
158 dest_ip = (int(device_ip_list[0]) << 24) | (int(device_ip_list[1]) << 16) | (int(device_ip_list[2]) << 8) | int(device_ip_list[3])
159 print (
"dest ip: %s" % strIp)
162 stTransmissionType = MV_TRANSMISSION_TYPE()
163 memset(byref(stTransmissionType), 0, sizeof(MV_TRANSMISSION_TYPE))
165 stTransmissionType.enTransmissionType = MV_GIGE_TRANSTYPE_MULTICAST
166 stTransmissionType.nDestIp = dest_ip
167 stTransmissionType.nDestPort = 8787
169 ret = cam.MV_GIGE_SetTransmissionType(stTransmissionType)
171 raise Exception (
"set transmission type fail! ret [0x%x]" % ret)
174 ret = cam.MV_CC_StartGrabbing()
176 raise Exception (
"start grabbing fail! ret[0x%x]" % ret)
179 hThreadHandle = threading.Thread(target=work_thread, args=(cam,
None,
None))
180 hThreadHandle.start()
182 raise Exception (
"error: unable to start thread")
184 print (
"press Enter key to stop grabbing.")
191 ret = cam.MV_CC_StopGrabbing()
193 raise Exception (
"stop grabbing fail! ret[0x%x]" % ret)
196 ret = cam.MV_CC_CloseDevice()
198 raise Exception (
"close deivce fail! ret[0x%x]" % ret)
201 cam.MV_CC_DestroyHandle()
202 except Exception
as e:
204 cam.MV_CC_CloseDevice()
205 cam.MV_CC_DestroyHandle()
208 MvCamera.MV_CC_Finalize()